package top.shen33.yang.services.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.shen33.yang.model.entity.RbacPermission;
import top.shen33.yang.model.entity.RbacRole;
import top.shen33.yang.mapper.RbacRoleMapper;
import top.shen33.yang.model.entity.RbacRolePermission;
import top.shen33.yang.services.RbacPermissionService;
import top.shen33.yang.services.RbacRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author oreilly
 * @since 2018-08-23
 */
@Service
public class RbacRoleServiceImpl extends ServiceImpl<RbacRoleMapper, RbacRole>implements RbacRoleService {


    @Autowired
    private RbacPermissionService permissionService;


    @Override
    public Class clazz() {
        return RbacRole.class;
    }

    @Override
    @Transactional
    public void doAdd(RbacRole itemRbacRole) {

        itemRbacRole.insertOrUpdate();
        doUpdateRel(itemRbacRole);


    }

    @Override
    @Transactional
    public void doUpdate(RbacRole itemRbacRole) {

        itemRbacRole.insertOrUpdate();

        doUpdateRel(itemRbacRole);
    }



    private void doUpdateRel(RbacRole itemRbacRole) {
        Integer roleSid = itemRbacRole.getSid();
        List<RbacPermission> rbacPermissionList = itemRbacRole.getRbacPermissionList();
        if(rbacPermissionList !=null && rbacPermissionList.size()>0){


            //delete old
            QueryWrapper<RbacRolePermission> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",roleSid);
            new RbacRolePermission().delete(wrapper);


            //add new
            for (RbacPermission rbacPermission : rbacPermissionList) {

                Integer permissionSid = rbacPermission.getSid();
                RbacRolePermission rolePermission=new RbacRolePermission();
                rolePermission.setRoleId(roleSid);
                rolePermission.setPermissionId(permissionSid);
                rolePermission.insertOrUpdate();
            }

        }
    }

    @Override
    @Transactional
    public void doDelete(String[] ids) {

        QueryWrapper<RbacRole> ew = new QueryWrapper<>();
        ew.in("sid", ids);
        new RbacRole().delete(ew);
    }


    @Override
    public List<RbacRole> all(RbacRole itemRbacRole) {

        return this.baseMapper.all(itemRbacRole);
    }

    @Override
    @Transactional
    public void doImport(List<RbacRole> dataList) {

    }


    @Override
    public Page<RbacRole> pageInfo(Page page, RbacRole itemRbacRole) {

        List<RbacRole> dataList = this.baseMapper.pageInfo(page, itemRbacRole);
        page.setRecords(dataList);
        return page;
    }

//    @Override
//    public RbacRole detail(String sid) {
//
//        List<RbacPermission> rbacPermissionList = permissionService.permissionList(sid);
//
//        RbacRole rbacRole = getById(sid);
//        rbacRole.setRbacPermissionList(rbacPermissionList);
//        return rbacRole;
//    }

    @Override
    public List<RbacRole> getRoleListByUserId(String userId) {

        return this.baseMapper.getRoleListByUserId(userId);
    }

    @Override
    public RbacRole getOne(String id) {
        List<RbacPermission> rbacPermissionList = permissionService.permissionList(id);

        RbacRole rbacRole = getById(id);
        rbacRole.setRbacPermissionList(rbacPermissionList);
        return rbacRole;
    }
}
